//
//  MNetworkTool.swift
//  iOSweibo
//
//  Created by lzm on 16/5/12.
//  Copyright © 2016年 lzm. All rights reserved.
//

import UIKit
import AFNetworking
//请求方法枚举
enum RequestMethod: String{
    case GET = "GET"
    case POST = "POST"
}

class MNetworkTool: NSObject {
    //单例
    static let sharedInstance : MNetworkTool = MNetworkTool()
    
    //属性AFHttpSessionManager https://api.weibo.com/oauth2/access_token
    private let afnmanager : AFHTTPSessionManager = {
        let afn = AFHTTPSessionManager(baseURL: NSURL(string: "https://api.weibo.com/"))
        afn.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return afn
        
    }()
    
    
    
    
    
    
    //自己封装GET和POST方法
//    func requestGet(URLString: String, parameters: AnyObject?, success: ((NSURLSessionDataTask, AnyObject?) -> Void)?, failure: ((NSURLSessionDataTask?, NSError) -> Void)?){
//        afnmanager.GET(URLString, parameters: parameters, progress: nil, success: success, failure: failure)
//    }
//    
//    func requestPost(URLString: String, parameters: AnyObject?, success: ((NSURLSessionDataTask, AnyObject?) -> Void)?, failure: ((NSURLSessionDataTask?, NSError) -> Void)?){
//        afnmanager.POST(URLString, parameters: parameters, progress: nil, success: success, failure: failure)
    
//    }
    
    func request(method: RequestMethod,URLString: String, parameters: AnyObject?, success: ((NSURLSessionDataTask, AnyObject?) -> Void)?, failure: ((NSURLSessionDataTask?, NSError) -> Void)?)
    {
        if method == RequestMethod.GET{
            afnmanager.GET(URLString, parameters: parameters, progress: nil, success: success, failure: failure)
        }
        else
        {
            afnmanager.POST(URLString, parameters: parameters, progress: nil, success: success, failure: failure)
        }
    }
    
    

}
